var express = require('express');
var jwt = require('jsonwebtoken');
var router = express.Router();
var mongoose = require('mongoose');
var foodGood = require('../models/foodGoods');
var onlyGood = require('../models/onlyGoods');

var options = {
	user: 'lzc',
	pass: 'znn520lzc',
	auth: {
		authdb: 'admin'
	}
};

mongoose.connect('mongodb://127.0.0.1:27017/tuGou', options);

mongoose.connection.on('connected', function() {
	console.log('连接mongoose成功');
});

mongoose.connection.on('error', function() {
	console.log('连接mongoose失败');
});

mongoose.connection.on('disconnected', function() {
	console.log('mongoose断开连接');
});

router.post('/login', function(req, res, next) {
	
})

//所有菜品
router.post('/foodGoods/list', function(req, res, next) {
	var page = parseInt(req.body['page']);
	var pageSize = parseInt(req.body['pageSize']);
	
	if(isNaN(page) || isNaN(pageSize)) {
		res.json(jsonMsg('0001'));
		return;
	}

	var skip = (page - 1) * pageSize;

	var foodGoodsModel = foodGood.find({}).skip(skip).limit(pageSize);

	foodGoodsModel.exec(function(err, doc) {
		if(err) {
			res.json(jsonMsg());
		} else {
			var status = jsonMsg('0000');
			var info = {
				result: doc,
				page: page,
				pageSize: pageSize
			}
			res.json(mergeObj(status, info));
		}
	})
});

//首页菜品slider
router.post('/foodGoods/slider', function(req, res, next) {
	foodGood.find({}, function(err, doc) {
		if(err) {
			res.json(jsonMsg());
		}else {
			var status = jsonMsg('0000');
			var result = doc;
			var data = [];
			
			for (var i = 0;i < 5;i++) {
				var random = getRandomInt(0, result.length - 1);
				data.push(result[random]);
				result.splice(random, 1);
			}
			
			var info = {
				result: data
			}
			res.json(mergeObj(status, info));
		}
	})
});

//所有单品
router.post('/onlyGoods/list', function(req, res, next) {
	var page = req.body['page'],
			pageSize = req.body['pageSize'];
			
	if (isNaN(page) || isNaN(pageSize)) {
		res.json(jsonMsg('0001'));
		return
	}
	
	var skip = (page - 1) * pageSize;
	
	var onlyGoodsModel = onlyGood.find({}).skip(skip).limit(pageSize);
	
	onlyGoodsModel.exec(function(err, doc) {
		if (err) {
			res.json(jsonMsg());
		}else {
			var status = jsonMsg('0000');
			var info = {
				result: doc,
				page: page,
				pageSize: pageSize
			}
			res.json(mergeObj(status, info));
		}
	})
})

//兔抢购
router.post('/onlyGoods/panicBuy', function(req, res, next) {
	onlyGood.find({}, function(err, doc) {
		if (err) {
			res.json(jsonMsg());
		}else {
			var status = jsonMsg('0000');
			var result = doc;
			var data = [];
			
			for (var i = 0;i < 2;i++) {
				var random = getRandomInt(0, result.length - 1);
				data.push(result[random]);
				result.splice(random, 1);
			}
			
			var info = {
				result: data
			}
			res.json(mergeObj(status, info));
		}
	})
})

module.exports = router;

//状态码
function jsonMsg(code = '9999') {
	var msg = '';

	switch(code) {
		case '0000':
			msg = '操作成功';
			break;
		case '0001':
			msg = '请完善传入字段';
			break;
		case '9999':
			msg = '程序出现问题';
	}

	return {
		code: code,
		msg: msg
	}
}

//合并 对象
function mergeObj(o1, o2) {
	for(var key in o2) {
		o1[key] = o2[key]
	}
	
	return o1;
}

//获取随机整数
function getRandomInt(min, max) {
	return Math.floor(Math.random() * (max - min + 1) + min);
}